﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Config.UserManagement;
using Microsoft.Practices.Unity;
using ScrumTable.UI.ViewModel.Controller;
using ScrumTable.UI.ViewModel.Data;
using System.Linq;

#endregion

namespace ScrumTable.UI.ViewModel.View
{
    /// <summary>
    /// Description of the class
    /// </summary>
    public class ProjectSettingViewModel : ViewModelBase
    {
 
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private ScrumViewModelData _scrumData;
        private ObservableDomainCollection<ProjectViewModel, Project> _projects;
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------  
        /// <summary>
        /// Gets or sets the account manager.
        /// </summary>
        /// <value>The account manager.</value>
        [Dependency] 
        public LoginAccountManager AccountManager { get; set;}

        /// <summary>
        /// Gets or sets the scrum data.
        /// </summary>
        /// <value>The scrum data.</value>
        [Dependency]
        public ScrumViewModelData ScrumData
        {
            get
            {
                return _scrumData;
            }
            set
            {
                _scrumData = value;
                value.PropertyChanged += OnScrumDataChanged;
            }
        }

        
        /// <summary>
        /// Gets the projects.
        /// </summary>
        /// <value>The projects.</value>
        public  ObservableDomainCollection<ProjectViewModel, Project> Projects
        {
            get
            {
                if (ScrumData.DataContext == null)
                {
                    return null;
                }
                if( _projects != null)
                {
                    _projects.Dispose();
                }
                _projects = new ObservableDomainCollection<ProjectViewModel, Project>();
                _projects.Fill(ScrumData.DataContext.Projects, project => new ProjectViewModel(project, ScrumData));

                return _projects;
            }
        }

        /// <summary>
        /// Gets or sets the current iteration.
        /// </summary>
        /// <value>The current iteration.</value>
        public Iteration CurrentIteration
        {
            get
            {
                if (ScrumData.CurrentIteration != null || CurrentProject != null)
                {
                    return ScrumData.CurrentIteration;
                }
                return null;
            }
            set
            {
                if (value != null && value != ScrumData.CurrentIteration)
                {

                    ScrumData.CurrentIteration = value;
                    //AccountManager.CurrentLoginAccount.IterationId = value.Id;
                    SendPropertyChanged("CurrentIteration");

                    SendDateChanges();
                }
            }
        }

       

        /// <summary>
        /// Gets or sets the current project.
        /// </summary>
        /// <value>The current project.</value>
        public ProjectViewModel CurrentProject
        {
            get
            {
                if (ScrumData.CurrentProject == null)
                {
                    if (ScrumData.DataContext != null && ScrumData.DataContext.Projects.Count > 0)
                    {
                        ScrumData.CurrentProject = ScrumData.DataContext.Projects.First();
                    }
                }
                if (ScrumData.CurrentProject != null)
                {                    
                    return new ProjectViewModel(ScrumData.CurrentProject, ScrumData);
                }
                return null;
            }
            set
            {
                if (value != null && value.Original != ScrumData.CurrentProject)
                {
                    ScrumData.CurrentProject = (value == null) ? null : value.Original;


                    SendPropertyChanged("CurrentProject");
                    SendPropertyChanged("CurrentIteration");
                    SendDateChanges();
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether the current iteration has acces to the wss information.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has acces to the wss; otherwise, <c>false</c>.
        /// </value>
        public bool HasRights
        {
            get { return true; }
        }


        public bool InWizardMode
        {
            get { return NaviController.CurrentTarget.IsWizardTarget(); }
        }

        /// <summary>
        /// Gets or sets year of the start time of current iteration.
        /// </summary>
        /// <value>Year of the start time of current iteration.</value>
        public int FromYear
        {
            get
            {
               if( CurrentIteration == null) return 0;
                return CurrentIteration.StartTime.Year;
            }
            set
            {
                CurrentIteration.StartTime = new DateTime(value, ScrumData.CurrentIteration.StartTime.Month, ScrumData.CurrentIteration.StartTime.Day);
                CurrentIteration.Save();
                SendPropertyChanged("FromYear");
            }
        }

        /// <summary>
        /// Gets or sets the index of month of the start time of current iteration.
        /// </summary>
        /// <value>The index of month of the start time of current iteration.</value>
        public int FromMonthIndex
        {
            get
            {
                if (CurrentIteration == null) return 0;
                return CurrentIteration.StartTime.Month-1;
            }
            set
            {
                CurrentIteration.StartTime = GetMonth(ScrumData.CurrentIteration.StartTime.Year, value, ScrumData.CurrentIteration.StartTime.Day); 
                CurrentIteration.Save();
                SendPropertyChanged("FromMonthIndex");
            }
        }

        /// <summary>
        /// Gets or sets day start time of current iteration.
        /// </summary>
        /// <value>Day of the start time of current iteration.</value>
        public int FromDay
        {
            get
            {
                if (CurrentIteration == null) return 0;
                return CurrentIteration.StartTime.Day;
            }
            set
            {
                CurrentIteration.StartTime = new DateTime(ScrumData.CurrentIteration.StartTime.Year, ScrumData.CurrentIteration.StartTime.Month, value);
                CurrentIteration.Save();
                SendPropertyChanged("FromDay");
            }
        }



        /// <summary>
        /// Gets or sets year of the end time of current iteration.
        /// </summary>
        /// <value>Year of the end time of current iteration.</value>
        public int ToYear
        {
            get
            {
                if (CurrentIteration == null) return 0;
                return CurrentIteration.EndTime.Year;
            }
            set
            {
                CurrentIteration.EndTime = new DateTime(value, ScrumData.CurrentIteration.EndTime.Month, ScrumData.CurrentIteration.EndTime.Day);
                CurrentIteration.Save();
                SendPropertyChanged("ToYear");
            }
        }

        /// <summary>
        /// Gets or sets the index of month of the end time of current iteration.
        /// </summary>
        /// <value>The index of month of the end time of current iteration.</value>
        public int ToMonthIndex
        {
            get
            {
                if (CurrentIteration == null) return 0;
                return CurrentIteration.EndTime.Month - 1;
            }
            set
            {
                

                CurrentIteration.EndTime = GetMonth(ScrumData.CurrentIteration.EndTime.Year, value, ScrumData.CurrentIteration.EndTime.Day);
                CurrentIteration.Save();
                SendPropertyChanged("ToMonthIndex");
            }
        }

      
        /// <summary>
        /// Gets or sets day of the end time of current iteration.
        /// </summary>
        /// <value>Day of the end time of current iteration.</value>
        public int ToDay
        {
            get
            {
                if (CurrentIteration == null) return 0;
                return CurrentIteration.EndTime.Day;
            }
            set
            {
                CurrentIteration.EndTime = new DateTime(ScrumData.CurrentIteration.EndTime.Year, ScrumData.CurrentIteration.EndTime.Month, value);
                CurrentIteration.Save();
                SendPropertyChanged("ToDay");
            }
        }

        protected override void ReleaseManagedResources()
        {
            ScrumData.PropertyChanged -= OnScrumDataChanged;
            if (_projects != null)
            {
                _projects.Dispose();
            }
            base.ReleaseManagedResources();
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Send foreach Date-Field a PropertyChange Event
        /// </summary>
        public void SendDateChanges()
        {
            SendPropertyChanged("FromDay");
            SendPropertyChanged("FromMonthIndex");
            SendPropertyChanged("FromYear");


            SendPropertyChanged("ToDay");
            SendPropertyChanged("ToMonthIndex");
            SendPropertyChanged("ToYear");
            SendPropertyChanged("HasRights");
        }

        private DateTime GetMonth(int year, int value, int day)
        {
            day = Math.Min(DateTime.DaysInMonth(year, value + 1), day);
            return new DateTime(year, value + 1, day);
        }


        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        private void OnScrumDataChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
          
            if (e.PropertyName == "DataContext")
            {
                
                SendPropertyChanged("Projects");
                SendPropertyChanged("CurrentProject");
                SendPropertyChanged("CurrentIteration");
            }
        }


        #endregion
    }
}